var JsOper=require("../Tool/JsOper.js");
var DataTable=require("../Tool/CDataTableName.js");
var BasicModel={
	SetHotel:function(MySql,aHotel,aResponse)
	{
		var InsertCol=["HOTELCODE","HOTELNAME","ADDRESS"];
		var InsertVal=["?","?","?"];
		var InPara=[aHotel.iCode,aHotel.iName,aHotel.iAddr];
		MySql.InsertTableRow(DataTable.TableHotel,InsertCol,InsertVal,InPara,
			function(Rst){
				if(Rst<0){
					var DataRst={iRst:Rst,iNewId:0};
					aResponse?aResponse(DataRst):0;
					return;
				}
				else{
					var DataRst={iRst:0,iNewId:Rst};
					aResponse?aResponse(DataRst):0;
					return;
				}
			});
	},

	GetHotels:function(Mysql,aResponse)
	{
		Mysql.SelectTable(DataTable.TableHotel,"","",false,function(Data)
		{
			if(Data===false){
  				var DataRst={iRst:-1,iHotels:[]};
  				aResponse?aResponse(DataRst):0;
  			}
	  		var Hotels=[];
  			for(var i in Data)
	  		{
                Hotels.push(
                {
                    iCode:Data[i].HOTELCODE,
                    iName :Data[i].HOTELNAME,
                    iAddr : Data[i].ADDRESS
                });
	  		}
	  		var DataRst={iRst:0,iHotels:Hotels};
			aResponse?aResponse(DataRst):0;
		});
	},

	NewRoomType:function(MySql,aRoomType,aResponse)
	{
		var InsertCol=["HOTELCODE","TYPENAME","PRICE"];
		var InsertVal=["?","?","?"];
		var InPara=[aRoomType.iCode,aRoomType.iTypeName,0];
		MySql.InsertTableRow(DataTable.TableRoomType,InsertCol,InsertVal,InPara,
			function(Rst){
				if(Rst<0){
					var DataRst={iRst:Rst,iNewId:0};
					aResponse?aResponse(DataRst):0;
					return;
				}
				else{
					var DataRst={iRst:0,iNewId:Rst};
					aResponse?aResponse(DataRst):0;
					return;
				}
			});
	},

	UpdateTypeName:function(MySql,aRoomType,aResponse)
	{
		var UpdateCol=["TYPENAME"];
		var UpdateVal=["?"];
		var InPara=[aRoomType.iTypeName];
		MySql.UpdateTable(DataTable.TableRoomType,UpdateCol,UpdateVal,"ID="+aRoomType.iTypeId,InPara,
			function(Rst){
				if(Rst<0){
					var DataRst={iRst:Rst,iNewId:0};
					aResponse?aResponse(DataRst):0;
					return;
				}
				else{
					var DataRst={iRst:0,iNewId:Rst};
					aResponse?aResponse(DataRst):0;
					return;
				}
			});
	},

	DeleteRoomType:function(MySql,aTypeId,aResponse)
	{
		MySql.DeleteTable(DataTable.TableRoomType,"ID="+aTypeId,false,
			function(Rst){
				if(Rst<0){
					var DataRst={iRst:Rst,iNewId:0};
					aResponse?aResponse(DataRst):0;
					return;
				}
				else{
					MySql.DeleteTable(DataTable.TableRoom,"TYPEID="+aTypeId,false,false);
					var DataRst={iRst:0,iNewId:Rst};
					aResponse?aResponse(DataRst):0;
					return;
				}
			});
	},

	GetRoomType:function(Mysql,aCode,aResponse)
	{
		var Clause=" HOTELCODE=? ";
		var Param=[aCode];
		Mysql.SelectTable(DataTable.TableRoomType,"",Clause,Param,function(Data)
		{
			if(Data===false){
  				var DataRst={iRst:-1,iTypes:[]};
  				aResponse?aResponse(DataRst):0;
  			}
	  		var Types=[];
  			for(var i in Data)
	  		{
                Types.push(
                {
                    iId :Data[i].ID,
                    iCode:Data[i].HOTELCODE,
                    iTypeName :Data[i].TYPENAME,
                    iPrice : Data[i].PRICE
                });
	  		}
	  		var DataRst={iRst:0,iTypes:Types};
			aResponse?aResponse(DataRst):0;
		});
	},

	SetHotelRoom:function(MySql,aHotelCode,aRoom,aResponse)
	{
		var InsertCol=["HOTELCODE","ZIMMER","TYPEID","IS_DIRTY","HAS_GUEST","NO_INTER","EXCEPT_STATUS"];
		var InsertVal=["?","?","?",0,0,0,0];
		var InPara=[aHotelCode,aRoom.iRoomNo,aRoom.iRoomType];
		MySql.InsertTableRow(DataTable.TableRoomInfo,InsertCol,InsertVal,InPara,
			function(Rst){
				if(Rst<0){
					var DataRst={iRst:Rst,iNewId:0};
					aResponse?aResponse(DataRst):0;
					return;
				}
				else{
					var DataRst={iRst:0,iNewId:Rst};
					aResponse?aResponse(DataRst):0;
					return;
				}
			});
	},

	GetHotelRoomByType:function(Mysql,aCode,aTypeId,aResponse)
	{
		var Clause=" HOTELCODE=? ";
		if(aTypeId>0) Clause=Clause+" AND TYPEID="+aTypeId;
		Clause=Clause+" ORDER BY ZIMMER ";
        var Param=[aCode];
		Mysql.SelectTable(DataTable.TableRoomInfo,"",Clause,Param,function(Data)
		{
			if(Data===false){
  				var DataRst={iRst:-1,iRooms:[]};
  				aResponse?aResponse(DataRst):0;
  			}
	  		var Rooms=[];
  			for(var i in Data)
	  		{
                Rooms.push(
                {
                    iId :Data[i].ID,
                    iRoomNo :Data[i].ZIMMER,
                    iRoomType : Data[i].TYPEID,
                    iHasGuest:Data[i].HAS_GUEST,
                    iIsDirty:Data[i].IS_DIRTY,
                    iNoInterrupt:Data[i].NO_INTER,
                    iExcept:Data[i].EXCEPT_STATUS
                });
	  		}
	  		var DataRst={iRst:0,iRooms:Rooms};
			aResponse?aResponse(DataRst):0;
		});
	}
};
exports.BasicModel=BasicModel;